package com.example.rpc.test;

import com.example.rpc.info.RpcInfo;

import java.io.ObjectInputStream;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * @description:
 * @author: RockyZZB
 * @time: 2020/12/21 21:24
 */
public class Serverhandle implements Runnable{
    private Socket socket;
    public Serverhandle(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            ObjectInputStream objectInputStream=new ObjectInputStream(socket.getInputStream());
            RpcInfo rpcInfo=(RpcInfo) objectInputStream.readObject();
            Class<?> aClass=Class.forName(rpcInfo.getPackageName()+"."+rpcInfo.getClazzName());
            Class[] classes=new Class[rpcInfo.getArgs().length];
            for (int i=0;i<classes.length;i++){
                classes[i]=rpcInfo.getArgs()[i].getClass();
                Method method = aClass.getMethod(rpcInfo.getMethodName(), classes);
                method.invoke(aClass.newInstance(),rpcInfo.getArgs());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
